package demo;

import dao.CourseDao;
import dao.ScoreDao;
import dao.StudentDao;
import model.Course;
import model.Score;
import model.Student;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.List;
import java.util.Scanner;

/**
 * @author tongchen
 * @create 2022-12-29 11:45
 */
public class exer5 {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        while (true) {
            CourseDao courseDao = new CourseDao();
            List<Course> courses = courseDao.selectAll();
            if(courses==null||courses.isEmpty()){
                System.out.println("本次查询为空，轻稍后再试");
                continue;
            }
            for (Course course:courses) {
                String format = MessageFormat.format("id:{0}\t|name:{1}\t",course.getId(),course.getName());
                System.out.println(format);
            }
            System.out.println("请输入学生姓名");
            String name=scanner.next();
            StudentDao studentDao = new StudentDao();
            List<Student> students = studentDao.selectOne(name);
            if(students==null||students.isEmpty()){
                System.out.println("没有该学生");
                return;
            }
            for (Student student:students) {
                String format = MessageFormat.format("学生ID:{0}\t|sn:{1}\t|name:{2}\t|email:{3}\t|classid:{4}\t|",
                        student.getId(), student.getSn(), student.getName(), student.getEmail(), student.getClassId());
                System.out.println(format);
            }
            System.out.println("请输入学生编号：");
            int studentId = scanner.nextInt();

            System.out.println("请输入课程编号：");
            int courseId = scanner.nextInt();
            System.out.println("请输入分数：");
            BigDecimal score=new BigDecimal(scanner.next());
            // 打印日志
            System.out.println(studentId + "-" + courseId + "-" + score);
            // 构建Score对象
            Score scoreObj = new Score();
            scoreObj.setScore(score);
            scoreObj.setStudentId(studentId);
            scoreObj.setCoourseId(courseId);
            // 调和ScoreDao完成插入操作
            ScoreDao scoreDao = new ScoreDao();
            int i = scoreDao.newScore(scoreObj);
            if(i>0){
                System.out.println("本次插入成功");
            }
                else{
                    System.out.println("本次插入失败");
                }
            }
        }
    }

